var flag = 0;

function getCourseList() {
    var terms = getApp().globalData.userInfo.terms;
    flag = 0;
    for (var i in terms) {
       course_get(i, terms[i]);
    }
}

function course_get(index, termCode, cb) {
    var course = null;
    let url = 'study/get_courses';
    // console.log("termCode:" + termCode);
    // console.log("flag:" + flag);
    getApp().xutils.request(url, {
        termCode
    }).then(res => {
        // console.log('获取课程中');
        const courset = wx.getStorageSync('course');
        if (courset == '') {
            course = new Array(10);
        } else {
            course = courset;
        }
        course.splice(index, 1, course_manager(res.data.data));
        wx.setStorageSync('course', course)
        if (cb && typeof cb == 'function') {
            getApp().globalData.course = course;
            cb();
        }
        flag++;
        // console.log("flag:"+flag);
        // console.log("termCode:" + termCode);
        if (flag == getApp().globalData.userInfo.terms.length || index == getApp().globalData.userInfo.term) { //将所有课程放入本地；
            wx.setStorage({
                key: 'userInfo',
                data: getApp().globalData.userInfo,
            });
            getApp().globalData.course = course;
        }
    }).catch(errmsg => {
        getApp().xutils.show_message(errmsg);
    });
}

function course_manager(course0) {

    var weekday = [ //一个周的七天
        [],
        [],
        [],
        [],
        [],
        [],
        []
    ];
    for (i in course0) {

        var str = course0[i].courseDetailInfo;
        var s = ",";
        var j = 0;
        var index_comma = []; //','在课程信息中出现的位置
        for (; str.indexOf(s, j) != -1;) {
            /*通过‘,’判断课程有几个上课地点,当','之前出现数字（上课地点的数字）时，可判断此逗号为课程上课地点分割。目前位置，所有课程均为一或二个上课地点*/
            var index = str.indexOf(s, j);
            index_comma.push(index);
            j = str.indexOf(s, j) + s.length;
        }
        for (var k in index_comma) {
            // (!isNaN(str.charAt(index_comma[k] - 1))) || (str.charAt(index_comma[k] - 1) == '）')
            //将分隔上课周数的逗号换为分号，下一步用分号来切分，包括两种情况，一种逗号之前为数字，另一种逗号之前为中文括号
            //已上分割方式不全，会出现逗号前面是上课地点的情况
            if (str.charAt(index_comma[k] - 1) != '周' && str.charAt(index_comma[k] - 1) != ')') {
                //当逗号前面为周或者(双)(单)时都不用替换
                str = str.substring(0, index_comma[k]) + ";" + str.substring(index_comma[k] + 1, str.length); //将逗号替换为分号，下一步用来切分上课地点和时间。但是当逗号之前是周时，表示的是间隔上课周数，不需要替换
                course0[i].courseDetailInfo = str;
            }
        }

    }


    var course, course1 = [], //course1已安排上课时间课程数组
        course2 = []; //未安排上课时间课程数组
    for (i in course0) {
        if (course0[i].courseDetailInfo == 'null') {
            course = {};
            course.courseName = course0[i].courseName;
            if (course0[i].courseTeacher == 'null') {
                course.courseTeacher = '未安排教师';
            } else {
                course.courseTeacher = course0[i].courseTeacher;
            }
            course2.push(course); //将未安排上课时间课程放入course2
        } else {
            var info = course0[i].courseDetailInfo.split(';'); //将有两个上课地点的课程分成两个课程
            for (j in info) {
                var course = {};
                course.courseName = course0[i].courseName;
                course.courseTeacher = course0[i].courseTeacher;
                if (course.courseTeacher == 'null')
                    course.courseTeacher = '未安排';
                course.courseDetailInfo = info[j];
                course1.push(course); //将已安排上课时间课程放入course2
            }
        }
    }
    // debugger;
    for (i in course1) {
        info = course1[i].courseDetailInfo.split(" ");
        var course = {};
        course.courseName = course1[i].courseName;
        course.courseTeacher = course1[i].courseTeacher;
        course.courseDetailInfo = info;
        course.courseTime = info[0];
        course.conflict = false;
        if (info[1] == '星期一' || info[1] == '星期1') {
            weekday[0].push(course);
        } else if (info[1] == '星期二' || info[1] == '星期2') {
            weekday[1].push(course);
        } else if (info[1] == '星期三' || info[1] == '星期3') {
            weekday[2].push(course);
        } else if (info[1] == '星期四' || info[1] == '星期4') {
            weekday[3].push(course);
        } else if (info[1] == '星期五' || info[1] == '星期5') {
            weekday[4].push(course);
        } else if (info[1] == '星期六' || info[1] == '星期6') {
            weekday[5].push(course);
        } else //周日
        {
            weekday[6].push(course);
        }
    }


    for (var i in weekday) {
        // if(i==6)
        // debugger;
        // console.log("week: ")
        // console.log(weekday[i])
        for (var j in weekday[i]) { //处理上课时间,每天的具体什么时候
            var time = weekday[i][j].courseDetailInfo[2];
            var index = time.indexOf("-");
            var course_begin = parseInt(time.substring(index - 1, index));
            var course_end = parseInt(time.substring(index + 1, index + 2));
            if (course_end == 1) //课程以两位数结束
                course_end = parseInt(time.substring(index + 1, index + 3));
            weekday[i][j].course_begin = course_begin;
            weekday[i][j].course_end = course_end;
            weekday[i][j].course_length = course_end - course_begin + 1;
        }
    }

    for (var i in weekday)
        for (var j in weekday[i]) { //处理上课周数


            var weekTime = new Array(25);
            for (var k = 0; k < weekTime.length; k++)
                weekTime[k] = false;
            var week = weekday[i][j].courseDetailInfo[0].split(",");

            for (var k in week) {
                if (week[k].indexOf("-") == -1) {
                    var index2 = week[k].indexOf("周");
                    weekTime[parseInt(week[k].substring(0, index2)) - 1] = true;
                } else {
                    var index1 = week[k].indexOf("-");
                    var weekBegin = parseInt(week[k].substring(0, index1));
                    var index2 = week[k].indexOf("周");
                    var weekEnd = parseInt(week[k].substring(index1 + 1, index2));
                    var index3 = week[k].indexOf("单");
                    var index4 = week[k].indexOf("双");
                    //记录单双周
                    if (index3 == -1 && index4 == -1) //单双周
                        for (; weekBegin <= weekEnd; weekBegin++) {
                            weekTime[weekBegin - 1] = true;
                        }
                    else if (index4 == -1) //单周
                        for (; weekBegin <= weekEnd; weekBegin++) {
                            if (weekBegin % 2 != 0)
                                weekTime[weekBegin - 1] = true;
                        }
                    else //双周 
                        for (; weekBegin <= weekEnd; weekBegin++) {
                            if (weekBegin % 2 == 0)
                                weekTime[weekBegin - 1] = true;
                        }
                }
            }
            weekday[i][j].weekTime = weekTime;
            //delete weekday[i][j].courseDetailInfo;
        }
    for (var i = 0; i < weekday.length; i++)
        for (var j = 0; j < weekday[i].length - 1; j++) {
            for (var k = j + 1; k < weekday[i].length; k++)
                //debugger;
                if ((weekday[i][j].course_begin == weekday[i][k].course_begin) && (weekday[i][j].course_end == weekday[i][k].course_end)) {
                    weekday[i][j].conflict = true;
                    weekday[i][k].conflict = true;
                }
        }
    var courseList = {};
    courseList.weekday = weekday;
    courseList.courseUnknowTime = course2;
    return courseList;
}

module.exports.course_get = course_get
module.exports.getCourseList = getCourseList